﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;
namespace CFM
{
    public partial class frmInput : Form
    {
        private ImportAPI _apiImport = null;
        public frmInput()
        {
            InitializeComponent();
            _apiImport = new ImportAPI();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmInputNew frm = new frmInputNew();
            frm.ShowDialog();
            grvImport.DataSource = _apiImport.GetAll();
        }

        private void frmInput_Load(object sender, EventArgs e)
        {
            grvImport.AutoGenerateColumns = false;
            grvImportDetail.AutoGenerateColumns = false;
            grvImport.DataSource = _apiImport.GetAll();
            cbxOptionSearch.SelectedIndex = 0;
        }

        private void grvImport_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            Import im = grvImport.SelectedRows[0].DataBoundItem as Import;
            List<ImportDetail> lsImportdetail = new List<ImportDetail>();
            lsImportdetail = _apiImport.GetImportDetailByImportId(im.ImportId);
            grvImportDetail.DataSource = lsImportdetail;
            //txtQuantity.Text = imp.Quantity.ToString();
            //cbxCategory.SelectedValue = imp.CategoryId;
            //cbxProduct.SelectedValue = imp.ProductId.Trim();
            MethodHelper.ClearErrorProvider();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cbxOptionSearch.Text == "--Tất cả--")
            {
                string keyword = txtKeyWord.Text;
                grvImport.DataSource = _apiImport.GetImportByAll(keyword);
            }
            else if(cbxOptionSearch.Text == "Nhà cung cấp")
            {
                string keyword = txtKeyWord.Text;
                grvImport.DataSource = _apiImport.GetImportByProviderName(keyword);
            }
            else if (cbxOptionSearch.Text == "Tổng tiền")
            {
                int keyword = Convert.ToInt32(txtKeyWord.Text.ToString());
                grvImport.DataSource = _apiImport.GetImportByTolal(keyword);
            }
            else if (cbxOptionSearch.Text == "Ngày lập")
            {
                string keyword = txtKeyWord.Text;
                grvImport.DataSource = _apiImport.GetImportByDate(keyword);
            }
            else if (cbxOptionSearch.Text == "Người lập")
            {
                string keyword = txtKeyWord.Text;
                grvImport.DataSource = _apiImport.GetImportByUerid(keyword);
            }
            else if (cbxOptionSearch.Text == "Mã hóa đơn")
            {
                string keyword = txtKeyWord.Text;
                grvImport.DataSource = _apiImport.GetImportByImportId(keyword);
            }  
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            grvImport.DataSource = _apiImport.GetAll();
        }
    }
}